﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenericEntities;
using System.Threading;

namespace DiaryPlanner
{
    public partial class PlannerEdit : Form
    {
        public const int DAY = 0;
        public const int WEEK = 1;
        public const int MONTH = 2;


        private const int morning = 0;
        private const int after = 1;

        private Planner planner;
        private PlannerDay plannerDay;
        private PlannerWeek plannerWeek;
        private PlannerMonth plannerMonth;

        public PlannerEdit()
        {
            InitializeComponent();
            skinSet();
        }

        public void skinSet()
        {
            string skin = UserProfile.Instance().setting;
            if (skin == "b")
            {
                this.BackColor = System.Drawing.Color.PaleTurquoise;

            }
            else if (skin == "c")
            {
                this.BackColor = System.Drawing.Color.LawnGreen;
            }
            else if (skin == "d")
            {
                this.BackColor = System.Drawing.Color.MistyRose;

            }
            else
            { //skin=="a"
                this.BackColor = System.Drawing.SystemColors.ControlLight;

            }
        }

        private void initPlannerEdit()
        {
            if (planner.genericEvent != null)
            {
                saveButton.Text = "수정";
                timeRegDate1.Value = planner.genericEvent.StartTime;
                timeRegDate2.Value = planner.genericEvent.EndTime;
                titleTextBox.Text = planner.genericEvent.Title;
                locationTextBox.Text = planner.genericEvent.Location;
                richTextBox1.Text = planner.genericEvent.Contents;
            }
            else if (plannerDay != null)
            {
                saveButton.Text = "저장";
                timeRegDate1.Value = planner.monthCalendar1.SelectionStart;
                timeRegDate2.Value = planner.monthCalendar1.SelectionEnd;

                timeRegDate1.Value = timeRegDate1.Value.AddHours(int.Parse(plannerDay.dayTime(morning).Substring(0, plannerDay.dayTime(morning).IndexOf(":"))));
                timeRegDate1.Value = timeRegDate1.Value.AddMinutes(int.Parse(plannerDay.dayTime(morning).Substring(plannerDay.dayTime(morning).IndexOf(":") + 1)));

                timeRegDate2.Value = timeRegDate2.Value.AddHours(int.Parse(plannerDay.dayTime(after).Substring(0, plannerDay.dayTime(after).IndexOf(":"))));
                timeRegDate2.Value = timeRegDate2.Value.AddMinutes(int.Parse(plannerDay.dayTime(after).Substring(plannerDay.dayTime(after).IndexOf(":") + 1)));
            }
            else if (plannerWeek != null)
            {
                saveButton.Text = "저장";
                timeRegDate1.Value = plannerWeek.getDate();
                timeRegDate2.Value = plannerWeek.getDate();

                timeRegDate1.Value = timeRegDate1.Value.AddHours(int.Parse(plannerWeek.weekTime(morning).Substring(0, plannerWeek.weekTime(morning).IndexOf(":"))));
                timeRegDate1.Value = timeRegDate1.Value.AddMinutes(int.Parse(plannerWeek.weekTime(morning).Substring(plannerWeek.weekTime(morning).IndexOf(":") + 1)));

                timeRegDate2.Value = timeRegDate2.Value.AddHours(int.Parse(plannerWeek.weekTime(after).Substring(0, plannerWeek.weekTime(after).IndexOf(":"))));
                timeRegDate2.Value = timeRegDate2.Value.AddMinutes(int.Parse(plannerWeek.weekTime(after).Substring(plannerWeek.weekTime(after).IndexOf(":") + 1)));
            }
            else
            {
                saveButton.Text = "저장";
                timeRegDate1.Value = planner.monthCalendar1.SelectionStart;
                timeRegDate2.Value = planner.monthCalendar1.SelectionEnd;
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (saveButton.Text == "저장") save();
            else if(saveButton.Text == "수정") update();
        }

        private void save()
        {
            GenericEvent ge = new GenericEvent();
            ge.Title = titleTextBox.Text;
            ge.Contents = richTextBox1.Text;
            ge.Location = locationTextBox.Text;
            ge.StartTime = timeRegDate1.Value;
            ge.EndTime = timeRegDate2.Value;

            CalendarThread ct;

            if(plannerDay != null) ct = new CalendarThread(planner,plannerDay, ge);
            else if (plannerWeek != null) ct = new CalendarThread(planner, plannerWeek, ge);
            else if (plannerMonth != null) ct = new CalendarThread(planner, plannerMonth, ge);
            else ct = new CalendarThread(planner, ge);

            ThreadStart ts = new ThreadStart(ct.addThread);
            Thread t = new Thread(ts);
            t.Start();

            this.Close();
        }

        private void update()
        {
            GenericEvent ge = new GenericEvent();
            ge.Title = titleTextBox.Text;
            ge.Contents = richTextBox1.Text;
            ge.Location = locationTextBox.Text;
            ge.StartTime = timeRegDate1.Value;
            ge.EndTime = timeRegDate2.Value;

            CalendarConnection.Instance().updateCalendar(planner.getSelectList(), ge);
            CalendarDB.Instance().update_db(planner.getSelectList(), ge);
            
            this.Close();
        }

        private DateTime parseDateTime(string time)
        {
            int index = 0;

            int year, month, day, hour, minute;
            
            year = int.Parse(time.Substring(0, index = time.IndexOf("-", index)));
            month = int.Parse(time.Substring(++index,2));
            index = index + 2;
            day = int.Parse(time.Substring(++index, 2));
            index = index + 2;
            if (time.Length == 16)
            {
                hour = int.Parse(time.Substring(++index, 2));
                index = index + 2;
                minute = int.Parse(time.Substring(++index, 2));
            }
            else
            {
                hour = int.Parse(time.Substring(++index, 1));
                index = index + 1;
                minute = int.Parse(time.Substring(++index, 2));
            }
            
            DateTime dateTime = new DateTime(year, month, day, hour, minute, 0);
            return dateTime;
        }

        public void initPlannerEdit(Planner planner)
        {
            this.planner = planner;
            initPlannerEdit();
        }

        public void initPlannerEdit(Planner planner, PlannerDay plannerDay)
        {
            this.plannerDay = plannerDay;
            initPlannerEdit(planner);
        }

        public void initPlannerEdit(Planner planner, PlannerWeek plannerWeek)
        {
            this.plannerWeek = plannerWeek;
            initPlannerEdit(planner);
        }

        public void initPlannerEdit(Planner planner, PlannerMonth plannerMonth)
        {
            this.plannerMonth = plannerMonth;
            initPlannerEdit(planner);
        }

        private void cancellButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
